import { ElMessage } from "element-plus"

/**
 * 将hex表示方式转换为rgb
 * @param {String} hexColor 颜色十六进制
 * @return {Array} [r, g, b]格式
 */
export function hexToRgb(hexColor: string) {
    const reg = /^#([0-9a-fA-f]{3}|[0-9a-fA-f]{6})$/
    let sColor = hexColor.toLowerCase()
    // 处理六位的颜色值
    const sColorChange = []
    if (sColor && reg.test(sColor)) {
        if (sColor.length === 4) {
            let sColorNew = '#'
            for (let i = 1; i < 4; i += 1) {
                sColorNew += sColor.slice(i, i + 1).concat(sColor.slice(i, i + 1))
            }
            sColor = sColorNew
        }

        for (let i = 1; i < 7; i += 2) {
            sColorChange.push(parseInt('0x' + sColor.slice(i, i + 2)))
        }
    }
    return sColorChange
}

/**
 * rgb颜色转Hex颜色
 * @param r 代表红色
 * @param g 代表绿色
 * @param b 代表蓝色
 * @returns 返回处理后的颜色值
 */
export function rgbToHex(r: any, g: any, b: any) {
    const reg = /^\d{1,3}$/
    if (!reg.test(r) || !reg.test(g) || !reg.test(b)) return ElMessage.warning('输入错误的rgb颜色值')
    const hexs = [r.toString(16), g.toString(16), b.toString(16)]
    for (let i = 0; i < 3; i++) if (hexs[i].length == 1) hexs[i] = `0${hexs[i]}`
    return `#${hexs.join('')}`
}
/**
 * rgba颜色转换成hex颜色和透明度
 * @param rgbaString 颜色值字符串
 * @returns 返回处理后的颜色信息
 */
export function rgbaStringToHex(rgbaString: string): { hexColor: string; alpha: number } | null {
    if (rgbaString.match(/[+-]?\d*\.\d+|\d+/g)) {
        const rgba = rgbaString.match(/[+-]?\d*\.\d+|\d+/g)?.map(Number) as number[]
        const r = Math.round(rgba[0] as number)
        const g = Math.round(rgba[1] as number)
        const b = Math.round(rgba[2] as number)
        const a = rgba[3] as number

        const hexR = r.toString(16).padStart(2, '0')
        const hexG = g.toString(16).padStart(2, '0')
        const hexB = b.toString(16).padStart(2, '0')
        const checkedA = a !== undefined && a !== null ? a : 1

        return {
            hexColor: `#${hexR}${hexG}${hexB}`,
            alpha: checkedA,
        }
    } else {
        return null
    }
}

/**
 * 生成渐变颜色值数组
 * @param {String} startColor 开始颜色值（十六进制）
 * @param {String} endColor 结束颜色值（十六进制）
 * @param {Number} count 生成颜色个数
 * @param {Number} step 生成步数
 * @return {Array} 渐变颜色数组
 */
export function gradientColor(startColor: string, endColor: string, count: number, step: number) {
    const startRGB: any = hexToRgb(startColor) // 转换为rgb数组模式
    const startR: any = startRGB[0]
    const startG: any = startRGB[1]
    const startB: any = startRGB[2]

    const endRGB: any = hexToRgb(endColor)
    const endR: any = endRGB[0]
    const endG: any = endRGB[1]
    const endB: any = endRGB[2]
    let flg = step
    if (step < 2) {
        flg = 0
    }
    const sR = (endR - startR) / (flg - 1) // 总差值
    const sG = (endG - startG) / (flg - 1)
    const sB = (endB - startB) / (flg - 1)
    const generateRStr = (r: any, g: any, b: any) => {
        return 'rgb(' + parseInt(r) + ',' + parseInt(g) + ',' + parseInt(b) + ')'
    }

    const colorArr: string[] = []
    for (let i = 0; i < step; i++) {
        // 计算每一步的rgb值
        // if (count % 2 !== 0 && i === 0) {
        //   colorArr.push(generateRStr(startR, startG, startB))
        // } else if (count % 2 !== 0 && i === step - 1) {
        //   colorArr.push(generateRStr(endR, endG, endB))
        // } else {
        const R = sR * i + startR
        const G = sG * i + startG
        const B = sB * i + startB
        colorArr.push(generateRStr(R, G, B))
        // }
    }
    return colorArr
}
/**
 * 根据一组颜色生成渐变色
 * @param {Array} colorArr 颜色数组
 * @param {Number} count 生成颜色个数
 */
export function multipleGradientColor(colorArr: string[] = [], count = 5) {
    const arr: string[] = []
    const cLen = colorArr.length - 1
    if (count === 1) return [colorArr[0]]
    if (count === 2) return [colorArr[0], colorArr[cLen - 1]]

    const devid = Math.ceil(count / (cLen - 1)) // 每两个渐变色产生的颜色个数
    for (let i = 0, len = cLen - 1; i < len; i++) {
        const startColor = /^rgb\(\s*\d+\s*,\s*\d+\s*,\s*\d+\s*\)$/.test(colorArr[i] as string)
            ? rgbaStringToHex(colorArr[i] as string)?.hexColor
            : colorArr[i]
        const endColor = /^rgb\(\s*\d+\s*,\s*\d+\s*,\s*\d+\s*\)$/.test(colorArr[i + 1] as string)
            ? rgbaStringToHex(colorArr[i + 1] as string)?.hexColor
            : colorArr[i + 1]
        arr.push(...gradientColor(startColor as string, endColor as string, count, devid))
    }
    const removeLen = arr.length - count
    if (removeLen > 0) {
        // 移除多余 count 的颜色
        for (let j = removeLen; j > 0; j--) {
            arr.splice(j * devid, 1)
        }
    }

    return arr
}
/**
 * 判断颜色是否为浅色系
 * @param {String} rgbColor rgb颜色值
 * @return {Boolean} 返回 true or false
 */
export function isLightColor(rgbColor: string) {
    const splitdata: any = rgbColor.replace(/(?:\(|\)|rgb|RGB)*/g, '').split(',')
    const level = splitdata[0] * 0.299 + splitdata[1] * 0.587 + splitdata[2] * 0.114
    if (level >= 192) {
        return true
    }
    return false
}
